package com.ssy.lingxi.search.controller.mobile;

import com.ssy.lingxi.common.constant.tree.Node;
import com.ssy.lingxi.common.controller.BaseController;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.model.vo.PageVO;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.search.api.model.request.BrandSearchRequest;
import com.ssy.lingxi.search.api.model.request.CommoditySearchRequest;
import com.ssy.lingxi.search.api.model.request.ShopSearchRequest;
import com.ssy.lingxi.search.api.model.request.SimpleIdListRequest;
import com.ssy.lingxi.search.api.model.response.*;
import com.ssy.lingxi.search.common.tree.BrandNode;
import com.ssy.lingxi.search.model.response.CategoryBrandResponse;
import com.ssy.lingxi.search.service.ICommodityService;
import com.ssy.lingxi.search.service.ITemplateService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

/**
 * @menu: APP-企业商城
 * 企业商城类
 * @author jw.chen
 * @version 2.0.0
 * @date 2020/8/1
 */
@RestController
@RequestMapping("/search/mobile/shop/enterprise")
public class MobileEnterpriseController extends BaseController {

    @Resource
    private ICommodityService commodityService;

    @Resource
    private ITemplateService templateCommodityService;

    /**
     * 查询一级商品品类
     */
    @RequestMapping(value = "/getFirstCategory", method = RequestMethod.GET)
    public Wrapper<List<SimpleNameUrlResponse>> getFirstCategory() {
        Long shopId = this.getHeadersShopId();
        return Wrapper.success(commodityService.getFirstCategory(shopId, null));
    }

    /**
     * 查询商品品类树
     */
    @RequestMapping(value = "/getCategoryTree", method = RequestMethod.GET)
    public Wrapper<List<Node>> getCategoryTree() {
        Long shopId = this.getHeadersShopId();
        return Wrapper.success(commodityService.getCategoryTree(shopId, null));
    }

    /**
     * 查询商品属性
     * @param categoryId 品类id
     */
    @RequestMapping(value = "/getAttributeByCategoryId", method = RequestMethod.GET)
    public Wrapper<List<AttributeResponse>> getAttributeByCategoryId(@RequestParam(value = "categoryId") Long categoryId) {
        Long shopId = this.getHeadersShopId();
        return Wrapper.success(commodityService.getAttributeByCategoryId(shopId, null, categoryId));
    }

    /**
     * 查询商品品牌
     */
    @RequestMapping(value = "/getBrand", method = RequestMethod.GET)
    public Wrapper<List<BrandResponse>> getBrand() {
        Long shopId = this.getHeadersShopId();
        return Wrapper.success(commodityService.getBrand(shopId, null));
    }

    /**
     * 查询商品品牌-按首字母排序
     */
    @RequestMapping(value = "/getPinyinSortBrand", method = RequestMethod.GET)
    public Wrapper<List<BrandNode>> getPinyinSortBrand() {
        Long shopId = this.getHeadersShopId();
        return Wrapper.success(commodityService.getPinyinSortBrand(shopId, null));
    }

    /**
     * 查询商品品牌列表
     * @param brandSearchRequest 参数
     */
    @RequestMapping(value = "/getBrandList", method = RequestMethod.POST)
    public Wrapper<PageData<BrandResponse>> getBrandList(@RequestBody BrandSearchRequest brandSearchRequest){
        Long shopId = this.getHeadersShopId();
        CommoditySearchRequest commoditySearchRequest = this.modelMapper.map(brandSearchRequest, CommoditySearchRequest.class);
        commoditySearchRequest.setShopId(shopId);

        //构造查询条件
        PageVO pageVO = new PageVO();
        pageVO.setCurrent(brandSearchRequest.getCurrent());
        pageVO.setPageSize(brandSearchRequest.getPageSize());

        return Wrapper.success(templateCommodityService.searchBrandList(pageVO, commoditySearchRequest));
    }

    /**
     * 查询归属地市
     */
    @RequestMapping(value = "/getArea", method = RequestMethod.GET)
    public Wrapper<List<ProvinceResponse>> getArea() {
        Long shopId = this.getHeadersShopId();
        return Wrapper.success(commodityService.getArea(shopId, null));
    }

    /**
     * 查询商品列表
     * @param shopSearchRequest 查询条件实体
     */
    @RequestMapping(value = "/getCommodityList", method = RequestMethod.POST)
    public Wrapper<PageData<CommoditySearchResponse>> searchCommodityList(@RequestBody ShopSearchRequest shopSearchRequest) {
        Long shopId = this.getHeadersShopId();
        CommoditySearchRequest commoditySearchRequest = this.modelMapper.map(shopSearchRequest, CommoditySearchRequest.class);
        commoditySearchRequest.setShopId(shopId);
        //判断是否已登录
        if(this.isLogin()){
            UserLoginCacheDTO sysUser = this.getSysUser();
            commoditySearchRequest.setLoginMemberId(sysUser.getMemberId());
            commoditySearchRequest.setLoginMemberRoleId(sysUser.getMemberRoleId());
        }
        return Wrapper.success(commodityService.searchCommodityList(commoditySearchRequest, true, false));
    }

    /**
     * 根据品牌id查询品类下的品牌
     * @param simpleIdListRequest 品牌id
     */
    @RequestMapping(value = "/getCategoryBrand", method = RequestMethod.POST)
    public Wrapper<List<CategoryBrandResponse>> getCategoryByBrand(@RequestBody SimpleIdListRequest simpleIdListRequest) {
        Long shopId = this.getHeadersShopId();
        List<Long> brandIdList = simpleIdListRequest.getIdList();
        List<CategoryBrandResponse> result = commodityService.getCategoryByBrand(brandIdList, shopId);
        return Wrapper.success(result);
    }

    /**
     * 根据一级品类查询品牌
     * @param customerCategoryId 一级品类id
     */
    @RequestMapping(value = "/getBrandByFirstCategory", method = RequestMethod.GET)
    public Wrapper<Set<BrandResponse>> getBrandByFirstCategory(@RequestParam("customerCategoryId") Long customerCategoryId) {
        Long shopId = this.getHeadersShopId();
        Set<BrandResponse> result = commodityService.getBrandByFirstCategory(customerCategoryId, shopId);
        return Wrapper.success(result);
    }

}
